﻿using HtmlAgilityPack;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Linq;
using System.Net.Http;

namespace HtmlAgilityPackAnalysis.ViewModels;

public class MainWindowViewModel : BindableBase
{
    private static HttpClient Http { get; } = new();
    private string _title = "网页内容解析器";

    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }

    public string RequestUri

    {
        get { return requestUri; }
        set { SetProperty(ref requestUri, value); }
    }

    private string requestUri = "http://www.baidu.com";

    public string Text
    {
        get { return text; }
        set { SetProperty(ref text, value); }
    }

    private string text;

    public string XPath
    {
        get { return xpath; }
        set { SetProperty(ref xpath, value); }
    }

    private string xpath = "//div";

    public string Attributes
    {
        get { return _attributes; }
        set { SetProperty(ref _attributes, value); }
    }

    private string _attributes;

    public string XPathName
    {
        get { return xpathName; }
        set { SetProperty(ref xpathName, value); }
    }

    private string xpathName = "a";

    public HtmlNodeCollection ChildNodes
    {
        get { return childNodes; }
        set { SetProperty(ref childNodes, value); }
    }

    private HtmlNodeCollection childNodes;

    public HtmlNode FirstChild
    {
        get { return firstChild; }
        set { SetProperty(ref firstChild, value); }
    }

    private HtmlNode firstChild;

    public DelegateCommand CommandGetString { get; set; }
    public DelegateCommand CommandAnalysis { get; set; }
    public DelegateCommand SelectionCommand { get; set; }
    public DelegateCommand EmptyCommand { get; set; }
    public DelegateCommand DescendantsCommand { get; set; }
    private readonly IDialogService DialogService;

    public MainWindowViewModel(IDialogService dialogService)
    {
        CommandGetString = new DelegateCommand(GetStringAsync);
        CommandAnalysis = new DelegateCommand(Analysis);
        SelectionCommand = new DelegateCommand(Selection);
        EmptyCommand = new DelegateCommand(Empty);
        DescendantsCommand = new DelegateCommand(Descendants);
        DialogService = dialogService;
    }

    private async void GetStringAsync()
    {
        Text = string.Empty;
        if (Uri.TryCreate(requestUri, UriKind.Absolute, out var uri))
        {
            try
            {
                Text = await Http.GetStringAsync(uri);
            }
            catch (Exception)
            {
            }
        }
    }

    private void Selection()
    {
        if (FirstChild != null)
        {
            Attributes = $"{{{Environment.NewLine}{string.Join(Environment.NewLine, FirstChild.Attributes.Select(a => $"{a.Name}: {a.Value}"))}{Environment.NewLine}}}";
        }
        else
        {
            Attributes = string.Empty;
        }
    }

    private void Analysis()
    {
        if (string.IsNullOrEmpty(Text))
        {
            return;
        }
        if (string.IsNullOrEmpty(xpath))
        {
            return;
        }
        try
        {
            HtmlDocument doc = new();
            doc.LoadHtml(Text);
            ChildNodes = doc.DocumentNode.SelectNodes(xpath);
            FirstChild = ChildNodes?.FirstOrDefault();
        }
        catch (Exception)
        {

           // throw;
        }
       
    }

    private void Descendants()
    {
        var data = FirstChild?.Descendants(XPathName);
        if (data?.Any() == true)
        {
            DialogService.ShowDialog("Descendants", new DialogParameters
            {
                { "data", data }
            }, null, "DialogWindow");
        }
    }

    private void Empty()
    {
        ChildNodes = null;
        FirstChild = null;
        Attributes = null;
    }
}